﻿using System;
using System.Threading.Tasks;
using CoreDemo.IServices;
using CoreDemo.MediatRs;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace CoreDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class demoController : ControllerBase
    {
        private readonly ISingletonDemo singletonDemo;
        private readonly IScopedDemo scopedDemo;
        private readonly ITransientDemo transientDemo;

        public demoController(IScopedDemo scopedDemo, ISingletonDemo singletonDemo, ITransientDemo transientDemo)
        {
            this.scopedDemo = scopedDemo;
            this.singletonDemo = singletonDemo;
            this.transientDemo = transientDemo;
        }

        [HttpGet]
        [Route("")]
        public ActionResult<int> getSingletonConut()
        {
            return singletonDemo.getConut();
        }

        [HttpGet]
        [Route("/getScopedConut")]
        public ActionResult<int> getScopedConut()
        {
            var a = scopedDemo.getConut();
            var b = scopedDemo.getConut();

            return a + b;
        }

        [HttpGet]
        [Route("/getTransientConut")]
        public ActionResult<int> getTransientConut()
        {
            var a = transientDemo.getConut();
            var b = transientDemo.getConut();


            return a + b;
        }
        public async void Aaa(HttpContext context)
        {
            IMediator mediator = context.RequestServices.GetRequiredService<IMediator>(); //可以使用依赖注入的方式获得mediator
            Ping ping = new Ping() { Title = "TestTitle" };
            string result = await mediator.Send(ping);
            await context.Response.WriteAsync(result);
        }

    }
}
